<?php
include 'checkSession.php';
//$lat=$_GET['lat'];
//$lon=$_GET['lng'];

$lat = 26.927799;
$lon = -82.055199;

$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
// compensate for degrees longitude getting smaller with increasing latitude
$maxLon = $lon + rad2deg($rad/$R/cos(deg2rad($lat)));
$minLon = $lon - rad2deg($rad/$R/cos(deg2rad($lat)));

//$query="SELECT sp_id,sp_latitude,sp_longitude, ( 6371 * acos( cos( radians( $lat ) ) * cos( radians( sp_latitude ) ) * cos( radians( sp_longitude ) - radians( $lon ) ) + sin( radians( $lat ) ) * sin( radians( sp_latitude ) ) ) ) AS distance, sp_latitude, sp_longitude FROM Sign_Posts HAVING distance <30 ORDER BY distance";
$query="Select sp_id,sp_latitude,sp_longitude, 
       acos(sin($lat)*sin(radians(sp_latitude)) + cos($lat)*cos(radians(sp_latitude))*cos(radians(sp_longitude)-$lon))*$R As D
From (
  Select sp_id,sp_latitude,sp_longitude
  From Sign_Posts
  Where sp_latitude>$minLat And sp_latitude<$maxLat
    And sp_longitude>$minLon And sp_longitude<$maxLon
  ) As FirstCut 
Where acos(sin($lat)*sin(radians(sp_latitude)) + cos($lat)*cos(radians(sp_latitude))*cos(radians(sp_longitude)-$lon))*$R < $rad
Order By D";
echo $query;
$res = mysql_query($query);

for($i=0;$i<mysql_num_rows($res);$i++) {
$row = mysql_fetch_array($res);
if($row['distance']>=.1) {
$distance=ROUND($row['distance'],2);
$sp_id=$row['sp_id'];
echo $sp_id."<br>";
}}


?>